<?php

/**
 *
 * Copyright (c) 2011 Tomáš Holub (tomas.holub@webadvice.cz)
 * 
 * @author Tomáš Holub (tomas.holub@webadvice.cz)
 * @package OverFishCMS
 */

namespace AdminModule\Forms;

use Nette\Application\UI\Form;

class UserForm extends Form {
    
    public function __construct($parent, $name)
    {
        parent::__construct($parent, $name);

        $this->addHidden("id");
        $this->addText('username', 'Uživatelské jméno:')
                ->setRequired('Uživatelské jméno musí být vyplněno')
                ->setAttribute("class",  "inp-form");
        $this->addText('fullname', 'Jméno:')
                ->setRequired('Jméno musí být vyplněno')
                ->setAttribute("class",  "inp-form");
        $this->addCheckbox("active", "Aktivní");
        $this->addSubmit('send', 'Odeslat');
        $this->onSuccess[] = array($this, 'formSubmited');
    }

    public function formSubmited($form)
    {
        if ($form->isValid()) {

            $values = $form->getValues();

            if (!$values->id){

                $user = $this->getPresenter()->entities->user;
                
                $user->setUsername($values->username);
                $user->setFullname($values->fullname);
                $user->setActive($values->active);
                
                $this->getPresenter()->repositories->user->save($user);
                
                $this->getPresenter()->flashMessage('Uživatel byl vytvořen.', 'success');
                
                $this->getPresenter()->redirect(':Admin:User:default');
            } else {
                
                $user = $this->getPresenter()->repositories->user->find($values->id);
   
                $user->setUsername($values->username);
                $user->setFullname($values->fullname);
                $user->setActive($values->active);
                
                $this->getPresenter()->repositories->user->save($user);
                
                $this->getPresenter()->flashMessage('Uživatel byl uložen.', 'success');
                
                $this->getPresenter()->redirect(':Admin:User:default');
            }
            
            
            
        }
    }
}

?>
